import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: "/login",
        component: () =>
            import ("../pages/login/login.vue"),
        meta: {
            title: "登录"
        }
    },
    {
        path: "/register",
        component: () =>
            import ("../pages/login/register.vue"),
        meta: {
            title: "注册"
        }

    },
    {
        path: "/home",
        component: () =>
            import ("../pages/home/home.vue"),

    },
    {
        path: "/detail",
        component: () =>
            import ("../pages/detail/detail.vue"),
        meta: {
            title: "商品详情"
        }
    },
    {
        path: "/search",
        component: () =>
            import ("../pages/search/search.vue"),
        meta: {
            title: "查询页面"
        }

    },
    {
        path: "/list",
        component: () =>
            import ("../pages/list/list.vue"),
        meta: {
            title: "商品列表",

            // keep true 缓存 没传递，undefined，就代表不想缓存
            keep: true
        }
    },
    {
        path: "/order",
        component: () =>
            import ("../pages/order/order.vue"),
        meta: {
            title: "确认订单",

        }
    },
    {
        path: "/site",
        component: () =>
            import ("../pages/site/site.vue"),
        meta: {
            title: "地址管理",

        }
    },
    {
        path: "/addsite",
        component: () =>
            import ("../pages/addsite/addsite.vue"),
        meta: {
            title: "编辑地址",
        }
    },
    {
        path: "/pay",
        component: () =>
            import ("../pages/pay/pay.vue"),
        meta: {
            title: "订单页面",
        }
    },
    {
        path: "/index",
        component: () =>
            import ("../pages/index/index.vue"),
        children: [{
                path: "sort",
                component: () =>
                    import ("../pages/index/sort.vue"),
                meta: {
                    title: "分类",
                    // keep true 缓存 没传递，undefined，就代表不想缓存
                    keep: true
                }
            },
            {
                path: "shopping",
                component: () =>
                    import ("../pages/index/shopping.vue"),
                meta: {
                    title: "购物车",
                    back: true,
                    // keep true 缓存 没传递，undefined，就代表不想缓存
                    keep: true
                }
            },
            {
                path: "store",
                component: () =>
                    import ("../pages/index/store.vue"),
                meta: {
                    title: "小u商城",
                    back: true,
                    // keep true 缓存 没传递，undefined，就代表不想缓存
                    keep: true
                }

            },
            {
                path: "main",
                component: () =>
                    import ("../pages/index/main.vue"),
                meta: {
                    title: "会员中心",
                    back: true
                }
            },
            {
                path: "",
                redirect: "store",

            }
        ]
    },
    //重定向
    {
        path: "*",
        redirect: "/login"
    },
]

const router = new VueRouter({
    routes,
    scrollBehavior(to, from, savePosition) {
        /*如果有历史记录，savePosition保留的是历史记录中的滚动位置；
        如果没有历史记录，savePostion是null .
        我们通过判断savePostion是否存在，存在就返回savePostion的位置；
        不存在，就返回（0,0，）*/
        if (savePosition) {
            return savePosition
        } else {
            return { x: 0, y: 0 }
        }

    }
})

//守卫
router.beforeEach((to, form, next) => {
    if (to.path === "/login" || to.path === "/register") {
        next()
        return
    }
    // 2.如果不是登录和注册，判断是否登录。 登陆了，next();
    let isLogin = localStorage.getItem("isLogin"); // 'true' null
    if (isLogin) {
        next()
        return;
    }
    // 3.没有登录，next("/login")
    next("/login")
})
export default router